package com.renu.jdependencyanalyser.window;

import com.renu.jdependencyanalyser.core.ClassBorderGene;
import com.renu.jdependencyanalyser.core.ProjectInfo;
import com.renu.jdependencyanalyser.data.ProjectDataCenter;
import com.renu.jdependencyanalyser.data.TblData;
import com.renu.jdependencyanalyser.freemarker.data.DependencyData;
import com.renu.jdependencyanalyser.freemarker.data.IdRelation;
import com.renu.jdependencyanalyser.model.ClassModel;
import com.renu.jdependencyanalyser.model.JarModel;
import com.renu.jdependencyanalyser.model.PackageModel;
import com.renu.jdependencyanalyser.utils.WebCrawler;
import com.renu.jdependencyanalyser.window.model.InnerClassTableItem;
import com.renu.jdependencyanalyser.window.model.InnerMethodTableItem;
import com.renu.jdependencyanalyser.window.model.JarTableItem;

import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PanelShow {
    private ProjectInfo projectInfo ;
    private String href;


    private AnalyserWindow analyserWindow = ProjectDataCenter.analyserWindow;

    public PanelShow(ProjectInfo projectInfo) {
        this.projectInfo = projectInfo;
    }

    public   void initCurrentClassPanel(ClassModel classModel) {

        JTextPane txt_classSig = analyserWindow.getTxt_classSig();
        JTable tb_classBorderMethod = analyserWindow.getTb_classBorderMethod();

        ClassBorderGene borderGene = new ClassBorderGene(projectInfo,classModel);
        String classSig  = classModel.getClsSig();

        initTxt_classSig(classSig);
        initTb_classOuterClass(classSig);
        initTb_classBorderMethod(borderGene);
    }

    private void initTb_classBorderMethod(ClassBorderGene borderGene) {
        JTable tb_classBorderMethod = analyserWindow.getTb_classBorderMethod();
        JTextPane txt_classBorderMethod = analyserWindow.getTxt_classBorderMethod();
        Set<String> dirOrIndirOuterMethodSig = borderGene.getDirOrIndirOuterMethodSig();
        int  dirOrIndirOuterMethodSigCnt = dirOrIndirOuterMethodSig.size();
        int method = borderGene.getClassModel().getMethodSigs().size();
        String[] header = {"method"};
        TblData<InnerMethodTableItem> tblData = new TblData<InnerMethodTableItem>(header);

        for (String s : dirOrIndirOuterMethodSig) {
            tblData.addItem(new InnerMethodTableItem(s));
        }
        tb_classBorderMethod.setModel(tblData.getTableModel());
        tb_classBorderMethod.setEnabled(true);
        txt_classBorderMethod.setText("dirOrIndirOuterMethods/AllMethods: " + dirOrIndirOuterMethodSigCnt +"/" + method);


    }

    private void initTb_classOuterClass(String classSig) {
        JTable tb_classOuterClass = analyserWindow.getTb_classOuterClass();
        JTextPane txt_classOuterClass = analyserWindow.getTxt_classOuterClass();

        DependencyData dependencyData = ProjectDataCenter.dependencyData;
        List<String> outerClassSig = dependencyData.getOuterSigs();
        int outerClsCnt = outerClassSig.size();
        int outDegree = 0;
        List<IdRelation> idRlts = dependencyData.getIdRlts();
        Integer classId = dependencyData.getAllMap().get(classSig);
        for (IdRelation idRlt : idRlts) {
            if (idRlt.getSrc() == classId){
                outDegree ++;
            }
        }
        txt_classOuterClass.setText("Dependent third party classes: " +outerClsCnt +"\n" +
                "Out-degree: " + outDegree);

        String [] header = {"Lib","Class"};
        TblData<InnerClassTableItem> tblData = new TblData<InnerClassTableItem>(header);
        for (String s : outerClassSig) {
            ClassModel classModel = projectInfo.getOuterClassMap().get(s);
            PackageModel packageModel = projectInfo.getOuterPckMap().get(classModel.getPck());
            JarModel jarModel = projectInfo.getJarModelMap().get(packageModel.getJarSig());
            String libSig = jarModel.getJarSig();
            tblData.addItem(new InnerClassTableItem(libSig,classModel.getClsSig()));
        }
        tb_classOuterClass.setModel(tblData.getTableModel());
        tb_classOuterClass.setEnabled(true);

    }

    private void initTxt_classSig(String clsSIg) {
        JTextPane txt_classSig = analyserWindow.getTxt_classSig();
        txt_classSig.setText(clsSIg);
    }

    public  void initOuterLib(JTable tb_outerLib, JTextPane txt_outerLib) {
        String [] header = {"Lib","Vulnerabilities","Usages"};
        TblData<JarTableItem> itemTblData = new TblData<JarTableItem>(header);
        Map<String, JarModel> borderJarMap = projectInfo.getBorderJarMap();
        Set<String> jarSig = borderJarMap.keySet();
        for (String s : jarSig) {
            JarModel jarModel = borderJarMap.get(s);

            Map<String, String> resMap = WebCrawler.crawlVulAndUsage(jarModel.getGroup(), jarModel.getArtifact(), jarModel.getVersion());
            String href = "";
            String vul = "";
            String usage = "";
            if (resMap.size()> 0){
                 href= resMap.get("Href");
                 vul = resMap.get("Vuln");
                 usage = resMap.get("Usage");
            }

            String libsig = jarModel.toString();
            JarTableItem jarTableItem = new JarTableItem(libsig, vul, usage);
            itemTblData.addItem(jarTableItem);

        }
        tb_outerLib.setModel(itemTblData.getTableModel());
        tb_outerLib.setEnabled(true);

        txt_outerLib.setText("Total" + jarSig.size());

    }
}
